package com.chezai.client.utils;

import com.chezai.client.bean.ImageInfo;
import com.chezai.client.global.TerminalApplication;
import com.chezai.client.global.location.LocationMgr;
import com.chezai.client.server.TerminalSocketClientManager;
import com.chezai.client.server.util.TransmissionMessageUtil;
import com.jx.driving.request.Terminal0x0900Request;
import com.jx.driving.transmission.Terminal0x0305Transmission;
import com.jx.driving.transmission.Terminal0x0306Transmission;

import java.io.File;

/**
 * Created by likunyang on 2017/9/21.
 */

public class TerminalUtils {
    /**
     * 初始化照片信息
     *
     * @param imageNo
     * @param buffer
     * @return
     */
    public static Terminal0x0305Transmission getTerminal0x0305Transmission(String imageNo, byte[] buffer, String unifiedNum, long classId, short uploadType, short eventType) {
        Terminal0x0305Transmission transmission = new Terminal0x0305Transmission();
        transmission.setImageNo(imageNo);
        transmission.setUserNo(unifiedNum);
        //1：平台拍照指令后上传自动请求上传；2：平台查询后要求上传；129：终端主动拍照上传；255：停止拍摄并上传图片
        transmission.setUploadType(uploadType);
        transmission.setCameraChannel((short) 0);
        transmission.setSize((short) 2);
        transmission.setEventType(eventType);
        transmission.setDataLength(buffer.length);
        transmission.setClassId(classId);
        transmission.setGnss(LocationUtils.getGnssPackage(LocationMgr.getBestLocation()));
        transmission.setTrustable((short) 100);
        int packageCount = getImagePackageCount(buffer);
        transmission.setPackageCount(packageCount);
        transmission.setAttachementList(TerminalApplication.getTerminalContext().getLocationAttachement());
        return transmission;
    }

    public static int getImagePackageCount(byte[] buffer) {
        int DATA_LENGTH_PER_FRAME = com.jx.driving.message.Message.DATA_LENGTH_PER_FRAME;
        int packageCount = (buffer.length + 10) / DATA_LENGTH_PER_FRAME;
        if (((buffer.length + 10) % DATA_LENGTH_PER_FRAME) != 0) {
            packageCount++;
        }
        return packageCount;
    }

    public static ImageInfo getImageInfo(String photoPath) {
        String imgName = photoPath.substring(photoPath.lastIndexOf("/") + 1);
        String img[] = imgName.split("\\.");
        File file = new File(photoPath);
        byte[] buffer = FileUtil.fileToByte(file);
        return new ImageInfo(buffer, img[0]);
    }

    /**
     * 上传照片到服务器
     *
     * @param newBuffer
     * @param imgName
     */
    public static Integer sendToServer(byte[] newBuffer, String imgName) {
        try {
            TransmissionMessageUtil messageUtil = new TransmissionMessageUtil();
            Terminal0x0306Transmission transmission = new Terminal0x0306Transmission();
            transmission.setImageNo(imgName);
            transmission.setContent(newBuffer);
            Terminal0x0900Request req = messageUtil.buildTerminal0x0900Request(transmission, false, true);
            TerminalSocketClientManager.getInstance().tellAndSync(req);
            //tell的流水号
            int sequence = req.getSeq();
            //如果存在sequence
            return sequence;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
